package cn.huanzi.qch.springbootmybatis.controller;

import cn.huanzi.qch.springbootmybatis.constant.Constant;
import cn.huanzi.qch.springbootmybatis.model.ErrorTip;
import cn.huanzi.qch.springbootmybatis.model.MysqlBackups;
import cn.huanzi.qch.springbootmybatis.model.SuccessTip;
import cn.huanzi.qch.springbootmybatis.service.MysqlBackupsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author keven 数据库备份-方案二
 * @date 2023-02-10 11:07 上午
 * @Description
 */
public class MysqlBackupsController {

    @Value("${spring.datasource.username}")
    private String userName;

    /**
     * 数据库密码
     */
    @Value("${spring.datasource.password}")
    private String password;

    /**
     * 数据库url
     */
    @Value("${spring.datasource.url}")
    private String url;

    /**
     * Windows数据库备份地址
     */
    @Value("${spring.datasource.win-path}")
    private String windowsPath;

    /**
     * Linux数据库备份地址
     */
    @Value("${spring.datasource.linux-path}")
    private String linuxPath;

    /**
     * 服务对象
     */
    @Resource
    private MysqlBackupsService mysqlBackupsService;


    /**
     * 获取所有备份数据列表
     * @return
     */
    @GetMapping("/backupsList")
    public Object backupsList() {
        List<MysqlBackups> systemMysqlBackups = mysqlBackupsService.selectBackupsList();
        return new SuccessTip(systemMysqlBackups);
    }

    /**
     * MySQL备份
     * @return
     */
    @PostMapping("/mysqlBackups")
    public Object mysqlBackups() {
        String path ;
        // 获取操作系统名称
        String osName = System.getProperty("os.name").toLowerCase();
        if (Constant.isSystem(osName)) {
            // Windows
            path = this.windowsPath;
        } else {
            // Linux
            path = this.linuxPath;
        }
        // 数据库用户名
        String userName = this.userName;
        // 数据库密码
        String password = this.password;
        // 数据库地址
        String url = this.url;
        // 调用备份
        Object systemMysqlBackups = mysqlBackupsService.mysqlBackups(path, url, userName, password);
        return new SuccessTip(systemMysqlBackups);
    }

    /**
     * 恢复数据库
     * @param map
     * @return
     */
    @PutMapping("/rollback")
//    @ApiJsonObject(name = "恢复数据库", value = {
//            @ApiJsonProperty(name = "id", example = "1", value = "数据id", dataType = "long", required = true)})
//                           @ApiParam(value = "恢复数据库")
    public Object rollback(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        if (id == null) {
            return new ErrorTip(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为null，请重新尝试！");
        }
        // 数据库用户名
        String userName = this.userName;
        // 数据库密码
        String password = this.password;
        // 根据id查询查询已有的信息
        MysqlBackups smb = mysqlBackupsService.selectListId(id);
        // 恢复数据库
        Object rollback = mysqlBackupsService.rollback(smb, userName, password);
        // 更新操作次数
        mysqlBackupsService.update(smb);
        return new SuccessTip(rollback);
    }


}
